package com.taptech.doufu.presenter.home;

import android.app.Activity;
import android.content.Context;

import com.google.gson.Gson;
import com.taptech.doufu.constant.HomePageType;
import com.taptech.doufu.net.retrofit.api.RxJavaHelper;
import com.taptech.doufu.util.CacheUtil;
import com.taptech.doufu.bean.DFHomeCategoryBean;
import com.taptech.doufu.bean.base.BaseResponseBean;
import com.taptech.doufu.listener.CommmonImp;
import com.taptech.doufu.net.retrofit.api.ApiClient;
import com.taptech.doufu.net.retrofit.api.BaseSubscriber;
import com.taptech.doufu.util.GsonUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Wyatt on 2017/10/13/013.
 */

public class HomeCategoryPresenter {

    public interface OnHomeCategoryView{

        void onCategoryNext(List<DFHomeCategoryBean> list, boolean isCache);
    }

    public HomeCategoryPresenter(Context context,String pageType,OnHomeCategoryView onHomeCategoryView){
        this.onHomeCategoryView = onHomeCategoryView;
        mContext = context;
        this.pageType = pageType;
    }

    private OnHomeCategoryView onHomeCategoryView;
    private Context mContext;
    private String pageType;



    private String getUrl() {
        if (HomePageType.RAINBOW_FINISHED.equals(pageType)){
            return "/new_home/new_home/entrance?sexType=1&type=finished";
        }else if (HomePageType.COPYRIGHT.equals(pageType)){
            return "";
        } else if (HomePageType.RAINBOW.equals(pageType)) {
            return "/index.php/new_home/new_home/entrance?sexType=1";
        } else {
            return "/index.php/new_home/new_home/entrance?sexType=3";
        }
    }

    private String getCachePath() {
        if (HomePageType.RAINBOW_FINISHED.equals(pageType)){
            return CommmonImp.Home_novel_category_finished;
        }else if (HomePageType.COPYRIGHT.equals(pageType)){
            return CommmonImp.Home_novel_category_copyright;
        }else if(HomePageType.RAINBOW.equals(pageType)){
            return CommmonImp.Home_novel_category;
        }else{
            return CommmonImp.Home_novel_UN_category;
        }
    }



    public void loadCategoryCache(final String sexType) {
        Observable.create(new Observable.OnSubscribe<List<DFHomeCategoryBean>>() {
                    @Override
                    public void call(Subscriber<? super List<DFHomeCategoryBean>> subscriber) {
                        List<DFHomeCategoryBean> list = null;
                        try {
                            JSONObject json = CacheUtil.readJsonDataFromCache(new File(getCachePath()));
                            list = GsonUtil.jsonToList(json.getJSONArray("data").toString(), DFHomeCategoryBean[].class);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        subscriber.onNext(list);
                    }
                })
                .compose(RxJavaHelper.<List<DFHomeCategoryBean>>observeOnMainThread((Activity) mContext))
                .subscribe(new BaseSubscriber<List<DFHomeCategoryBean>>(){
                    @Override
                    public void onNext(List<DFHomeCategoryBean> list) {
                        super.onNext(list);
                        if (list != null && onHomeCategoryView!=null) {
                            onHomeCategoryView.onCategoryNext(list,true);
                        }
                    }

                });
    }
    /**
     * 获取分类数据彩虹、言情
     */
    public void getHomeNovelCategory(boolean isUseCache,final String sexType) {
        if (getUrl().isEmpty()) {
            if (onHomeCategoryView != null) {
                onHomeCategoryView.onCategoryNext(new ArrayList<>(),false);
            }
            return;
        }
        ApiClient.getInstance().setUseCache(isUseCache).setSexType(sexType).getService().getNovelCategory(getUrl())
                .map(new Func1<BaseResponseBean<List<DFHomeCategoryBean>>, BaseResponseBean<List<DFHomeCategoryBean>>>() {
                    @Override
                    public BaseResponseBean<List<DFHomeCategoryBean>> call(BaseResponseBean<List<DFHomeCategoryBean>> listBaseResponseBean) {
                        CacheUtil.saveJsonDataToCache(new Gson().toJson(listBaseResponseBean),new File(getCachePath()));
                        return listBaseResponseBean;
                    }
                })
                .compose(RxJavaHelper.<BaseResponseBean<List<DFHomeCategoryBean>>>observeOnMainThread((Activity) mContext))
                .subscribe(new BaseSubscriber<BaseResponseBean<List<DFHomeCategoryBean>>>() {
                    @Override
                    public void onNext(BaseResponseBean<List<DFHomeCategoryBean>> novelHomeCategoryBean) {
                        super.onNext(novelHomeCategoryBean);
                        if (onHomeCategoryView != null && novelHomeCategoryBean != null) {
                            onHomeCategoryView.onCategoryNext(novelHomeCategoryBean.getData(),false);
                        }
                    }
                });
    }

    /**
     * 获取分类数据 同人
     */
    public void getHomeNovelGAYCategory(boolean isUseCache) {
        ApiClient.getInstance().setUseCache(isUseCache).setSexType(ApiClient.SEX_TYPE_NO).getService().getNovelCpCategory(ApiClient.SEX_TYPE_NO)
                .map(new Func1<BaseResponseBean<List<DFHomeCategoryBean>>, BaseResponseBean<List<DFHomeCategoryBean>>>() {
                    @Override
                    public BaseResponseBean<List<DFHomeCategoryBean>> call(BaseResponseBean<List<DFHomeCategoryBean>> listBaseResponseBean) {
                        CacheUtil.saveJsonDataToCache(new Gson().toJson(listBaseResponseBean), new File(CommmonImp.Home_novel_gay_category));
                        return listBaseResponseBean;
                    }
                })
                .compose(RxJavaHelper.<BaseResponseBean<List<DFHomeCategoryBean>>>observeOnMainThread((Activity) mContext))
                .subscribe(new BaseSubscriber<BaseResponseBean<List<DFHomeCategoryBean>>>() {
                    @Override
                    public void onNext(BaseResponseBean<List<DFHomeCategoryBean>> novelHomeCategoryBean) {
                        super.onNext(novelHomeCategoryBean);
                        if (onHomeCategoryView != null && novelHomeCategoryBean != null) {
                            onHomeCategoryView.onCategoryNext(novelHomeCategoryBean.getData(),false);
                        }
                    }
                });
    }

}
